package com.boyuan.system.callback;

import com.boyuan.system.hik.HCNetSDK;
import com.boyuan.system.websocket.BlockQueue;
import com.sun.jna.Pointer;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

@Slf4j

public class ThermInfoCallback implements HCNetSDK.FRemoteConfigCallBack {
    @Override
    public void invoke(int dwType, Pointer pBuffer, int dwBufLen, Pointer pUserData) {
 
        log.info("dwType:"+dwType+",dwBufLen:"+dwBufLen+",pUserData:"+pUserData.getString(0));
 
        switch (dwType){
            case 0:
                HCNetSDK.REMOTECONFIGSTATUS struCfgStatus  = new HCNetSDK.REMOTECONFIGSTATUS();
                struCfgStatus.write();
                Pointer pCfgStatus = struCfgStatus.getPointer();
                pCfgStatus.write(0, pBuffer.getByteArray(0, struCfgStatus.size()), 0,struCfgStatus.size());
                struCfgStatus.read();
 
                int iStatus = 0;
                for(int i=0;i<4;i++)
                {
                    int ioffset = i*8;
                    int iByte = struCfgStatus.byStatus[i]&0xff;
                    iStatus = iStatus + (iByte << ioffset);
                }
 
                switch (iStatus){
                    case 1000:
                        log.error("实时测温回调成功,dwStatus:" + iStatus);
                        break;
                    case 1001:
                        log.error("正在获取实时测温回调数据中,dwStatus:" + iStatus);
                        break;
                    case 1002:
                        int iErrorCode = 0;
                        for(int i=0;i<4;i++)
                        {
                            int ioffset = i*8;
                            int iByte = struCfgStatus.byErrorCode[i]&0xff;
                            iErrorCode = iErrorCode + (iByte << ioffset);
                        }
                        log.error(pUserData.getString(0)+"获取实时测温回调数据失败, dwStatus:" + iStatus + "错误号:" + iErrorCode);
                        break;
                    default:
                        break;
                }
                break;
            case 2:
                HCNetSDK.NET_DVR_THERMOMETRY_UPLOAD m_struThermometryInfo = new HCNetSDK.NET_DVR_THERMOMETRY_UPLOAD();
                m_struThermometryInfo.write();
                Pointer pInfoV30 = m_struThermometryInfo.getPointer();
                pInfoV30.write(0, pBuffer.getByteArray(0, m_struThermometryInfo.size()), 0,m_struThermometryInfo.size());
                m_struThermometryInfo.read();
 
                if(m_struThermometryInfo.byRuleCalibType == 0){
                    System.out.println("点测温信息:"+ m_struThermometryInfo.struPointThermCfg.fTemperature);
                }
                if(m_struThermometryInfo.byRuleCalibType == 1 || m_struThermometryInfo.byRuleCalibType == 2){
                    Map<String,Float> map = new HashMap<>();
                    map.put("fMaxTemperature",m_struThermometryInfo.struLinePolygonThermCfg.fMaxTemperature);
                    map.put("fMinTemperatur",m_struThermometryInfo.struLinePolygonThermCfg.fMinTemperature);
                    map.put("fAverageTemperature",m_struThermometryInfo.struLinePolygonThermCfg.fAverageTemperature);
                    BlockQueue.ipToTemperatureMap.put(pUserData.getString(0), map);
                    log.info("框/线测温信息:fMaxTemperature__"+m_struThermometryInfo.struLinePolygonThermCfg.fMaxTemperature);
                    log.info("   fMinTemperature__"+m_struThermometryInfo.struLinePolygonThermCfg.fMinTemperature);
                    log.info("  fAverageTemperature__"+m_struThermometryInfo.struLinePolygonThermCfg.fAverageTemperature);
                    log.info("   fTemperatureDiff__"+m_struThermometryInfo.struLinePolygonThermCfg.fTemperatureDiff);
                    log.info("    fCenterPointTemperature__"+m_struThermometryInfo.fCenterPointTemperature);
                }
                break;
            default:
                break;
        }

    }
}